<template>
  <div class="table-all">
    <!-- 搜索区 -->
    <div class="app-container search-box">
      <SearchForm
        :form="searchData.form"
        :options="searchData.options"
        @searchUpdate="search"
        @resetSearchData="resetSearchData"
      ></SearchForm>
    </div>
    <!-- 按钮区 -->
    <div class="app-container table-box">
      <div class="table-main">
        <!-- 表格 -->
        <v-table
          title="政策制定辅助数据"
          v-if="tableMainHeight>0"
          :table-columns="tableColumns"
          :table-data="tableData"
          :loading="loading"
          :is-index="true"
          ref="table"
          :maxHeight="tableMainHeight">
          <template #topButtonList>
            <ButtonForm :buttonList="buttonList" @clickButton="clickButton"></ButtonForm>
          </template>
          <template #checkType="scope">
            {{getDictValue(scope.row.checkType, cityCheckTypeOptions)}}
          </template>
          <template #result="scope">
            {{ getResultLabel(scope.row)}}
          </template>
          <template #operation="scope">
            <!--<i class="el-icon-view table-operation" style="color: #4E89EF;"-->
               <!--@click="handleDetail(scope.row)"></i>-->
            <a v-hasPermission="['cityPolicyMake:update']" class="table-btn-edit table-operation"
               @click="handleEdit(scope.row)">{{$t('table.edit')}}</a>
          </template>
        </v-table>
      </div>
      <div class="table-page">
        <!-- 页码 -->
        <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                    @pagination="search"/>
      </div>
    </div>
    <!-- 详情 -->
    <detailDialog :dialog-visible="detailDialogShow"
                  @success="editSuccess"
                  @close="editClose"
                  :openType="detailOpenType"
                  :id="detailId"
                  :title="detailTitle"
                  :cityList = "cityList"
    ></detailDialog>
  </div>
</template>

<script>
  import { cityPolicyMakePage,
    cityPolicyMakeExport
  } from '@/api/risk/cityWaringResult'
  import { getAreaList } from '@/api/system/area'
  import SearchForm from '@/components/searchForm/index'
  import ButtonForm from '@/components/ButtonForm/index'
  import VTable from '@/components/VTable/index'
  import Pagination from '@/components/Pagination/index'
  import detailDialog from './detail.vue'
  export default {
    components:{
      VTable,Pagination,SearchForm,ButtonForm,detailDialog
    },
    data(){
      return{
        tableColumns:[
          {
            prop: 'reportName',
            label: '报告名称',
            minWidth: '150',
            slotname: 'reportName',
            options: {}
          },
          {
            prop: 'year',
            label: '指标年份',
            minWidth: '100',
            slotname: 'year',
            options: {}
          },
          {
            prop: 'cityName',
            label: '区域',
            minWidth: '100',
            slotname: 'CityName',
            options: {}
          },
          {
            prop: 'targetName',
            label: '涉及指标范围',
            minWidth: '150',
            slotname: 'targetName',
            options: {}
          },
          {
            prop: 'targetNo',
            label: '涉及指标数量',
            minWidth: '150',
            slotname: 'targetNo',
            options: {}
          },
          {
            prop: 'createTime',
            label: '生成时间',
            minWidth: '150',
            slotname: 'createTime',
            options: {}
          },
          {
            prop: 'updateTime',
            label: '编辑时间',
            minWidth: '150',
            slotname: 'updateTime',
            options: {}
          },
          {
            prop: 'updateBy',
            label: '编辑人',
            minWidth: '150',
            slotname: 'updateBy',
            options: {}
          },
          {
            prop: 'operation',
            label: '操作',
            width: '200',
            align: 'center',
            slotname: 'operation',
            overflow: false,
            fixed: 'right'
          }
        ],
        tableData:[],
        selection: [],
        loading:false,
        total:1,
        pageNum:1,
        pageSize:10,
        detailOpenType:"add",
        detailDialogShow:false,
        detailId:"",
        detailTitle:"新增",
        dict:{},
        searchData:{
          form:{
            cityCode:'',
            year:'',
            threeTargetName:'',
          },
          options:[
            {
              title:'报告名称',
              key: "reportName",
              type: "text",
            },
            {
              title:'年份',
              key: "year",
              type: "dateYear",
            },
            // {
            //   title:'城市',
            //   key: "cityCode",
            //   type: "select",
            //   multiple:false,
            //   option: {
            //     data: this.cityList,
            //     props: {
            //       key: "id", // 指定key对应data的字段
            //       label: "areaName", // 指定label对应data的字段
            //       value: "areaCode", // 指定value对应data的字段
            //     },
            //   },
            // }
          ]
        },
        buttonList:[
          // {name:'新增',methods:'handleAdd',type:'primary', hasPremission:["cityPolicyMake:add"]},
          {name:'导出数据',methods:'handleExport', type:'primary', hasPremission:["cityPolicyMake:export"]}
        ],
        tableMainHeight: 0,
        // 字典
        cityList:[]
      }
    },
    watch: {
      // 'cityList':{
      //   handler(newVal, oldVal){
      //     if(newVal){
      //       this.searchData.options.forEach(t=>{
      //         if(t.key == 'cityCode'){
      //           t.option.data = newVal
      //         }
      //       })
      //     }
      //   }
      // }
    },
    methods:{
      handleDetail(row){
        this.detailId = row.id
        this.detailOpenType = "detail"
        this.detailTitle="详情"
        this.detailDialogShow = true
      },
      getDictValue(value,option){
        let item = option.find(t=> t.value == value)
        if(item!=undefined){
          return item.label
        }
        return value
      },
      editClose(){
        this.detailDialogShow = false
      },
      editSuccess(){
        this.search()
        this.detailDialogShow = false
      },
      search(){
        this.fetch({
          ...this.searchData.form,
        })
      },
      fetch(params={}){
        params.pageSize = this.pageSize
        params.pageNum = this.pageNum
        this.loading = true
        cityPolicyMakePage(params).then(res=>{
          if(res.code==200){
            this.total = res.data.total
            this.tableData = res.data.rows
          }else{
            this.$message({
              message: this.$t('tips.systemException'),
              type: 'error'
            })
          }
        }).finally(final=>{
          this.loading = false
        })
      },
      // 重置搜索条件
      resetSearchData(){
        this.searchData.form = {
          cityCode:'',
          year:'',
          threeTargetName:'',
        }
      },
      // 触发按钮区域按钮方法
      clickButton(methods){
        this[methods]()
      },
      handleAdd(){
        this.detailOpenType = "add"
        this.detailTitle = "新增"
        this.detailDialogShow = true
      },
      handleEdit(row){
        this.detailId = row.id
        this.detailOpenType = "edit"
        this.detailTitle="编辑"
        this.detailDialogShow = true
      },
      handleExport(){
        let params = {
          ...this.searchData.form
        }
        cityPolicyMakeExport(params,`政策制定辅助_${new Date().getTime()}.xlsx`);
      },
      handleImport(){
        console.log("等待处理")
      },
      closeDialog(){
        this.detailDialogShow = false
      },
    },
    activated(){
      this.fetch()
    },
    created(){
      // 获取字典值
      getAreaList({parentCode:"440000000000"}).then(res=>{
        if(res.code==200){
          this.cityList = res.data
        }
      })
      this.$commonJs.setLeftMenuHidden(this)
    },
    mounted(){
      this.tableMainHeight = document.getElementsByClassName('table-main')[0].clientHeight - 44 //减去顶部按钮列表的高度
    }
  }
</script>

<style lang="scss" scoped>
  .table-all {
    display: flex;
    flex-direction: column;
    height: 100%;

    .search-box {
      padding-bottom: 6px;
    }

    .table-box {
      padding-top: 10px;
      flex: 1;
      display: flex;
      flex-direction: column;

      .table-main {
        flex: 1;
        overflow: auto;
      }

      .table-page {
        // padding-top: 10 px;
        // padding-bottom: 10 px;
      }

      ::v-deep .pagination-container {
        text-align: right;
      }

    }
  }
</style>
